﻿using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using MBWebService.Core.Enums;
using MediaBrowser.Library.Entities;
using MediaBrowser.Library;

namespace MBWebService.Core.Classes
{
    [DataContract]
    [ServiceKnownType(typeof(MetadataItem))]
    [ServiceKnownType(typeof(BasicCollectionItem))]
    public class BasicItem
    {
        private static readonly DateTime MinDate = new DateTime(1970, 1, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public BasicItem() { }

        public BasicItem(BaseItem item)
        {
            Id = item.Id;
            Name = item.Name;
            TypeName = item.GetType().Name;
            Type = TypeName.ToEnum<MediaTypes>();
            if (Type == MediaTypes.Unkown &&
                item is Folder)
            {
                Type = MediaTypes.Folder;
            }

            // Only set this if it's got a valid value else JSON can't parse it
            if (item.DateCreated == DateTime.MinValue)
            {
                item.DateCreated = MinDate;
            }

            DateCreated = item.DateCreated;
        }

        [DataMember]
        public Guid Id { get; set; }

        [DataMember]
        public DateTime? DateCreated { get; protected set; }

        [DataMember]
        public string Name { get; set; }

        // [DataMember] // Only expose this for debugging
        public string TypeName { get; set; }

        [DataMember]
        public MediaTypes Type { get; set; }

        [DataMember]
        public MediaModes Mode { get; set; }

        public override string ToString()
        {
            return Type + ": " + Name;
        }

        public static explicit operator BasicItem(BaseItem item)
        {
            return new BasicItem(item);
        }
    }
}
